//============================================================
//write by bxl at 2021-06-21 13:38:14
//============================================================


 
using System.Collections.Generic;
using System;
using BCService.Utils;
using BCCommon;
using BCData.CRM.BigCustomerFreightConfiguration;
using BCDto.CRM.BigCustomerFreightConfiguration;
using BCEntity.CRM.BigCustomerFreightConfiguration;
using BCDto.Sim.Admin.Admin;
using BCData.TMS.FreightConfiguration;
using BCEntity.Common.EntityNameConst;
using BCData.CRM.BigCustomer;
using BCData.TMS.Agent.AgentRecord;
using BCCommon.TMS.FreightEnums;
using BCCommon.TMS.Order;
using BCEntity.TMS.FreightConfiguration;
using BCDto.TMS.SpecificServiceType;

namespace BCService.CRM.BigCustomerFreightConfiguration
{
    public class BigCustomerFreightConfigurationService : IBigCustomerFreightConfigurationService
    {
        #region var
        private readonly IDatabaseContext databaseContext;
        private IBigCustomerFreightConfigurationData bigCustomerFreightConfigurationData;
        private readonly IFreightConfigurationData freightConfigurationData;
        private readonly IRedisService redisService;
        private readonly IBigCustomerData bigCustomerData;
        private readonly IAgentRecordData agentRecordData;

        private readonly string key = string.Format("{0}", EntityNameConst.BigCustomerFreightConfigurationEntity);
         



        #endregion

        #region 构造函数

        public BigCustomerFreightConfigurationService(IDatabaseContext databaseContext, IBigCustomerFreightConfigurationData bigCustomerFreightConfigurationData, IFreightConfigurationData freightConfigurationData, IRedisService redisService, IBigCustomerData bigCustomerData, IAgentRecordData agentRecordData)  
        {
            this.databaseContext = databaseContext;
            this.bigCustomerFreightConfigurationData = bigCustomerFreightConfigurationData;
            this.freightConfigurationData = freightConfigurationData;
            this.redisService = redisService;
            this.bigCustomerData = bigCustomerData;
            this.agentRecordData = agentRecordData;
        }

        #endregion
        
        #region Add
        public BigCustomerFreightConfigurationDto Add(BigCustomerFreightConfigurationRequestDto bigCustomerFreightConfigurationRequestDto, AdminDto admin)
        {
            var org = bigCustomerFreightConfigurationData.GetEntityAsync(bigCustomerFreightConfigurationRequestDto.FreightConfigurationId, bigCustomerFreightConfigurationRequestDto.BigCustomerId).GetAwaiter().GetResult();
            if (org != null)
                throw new ArgumentException("要添加的记录重复!");

            var bigCustomerEntity = bigCustomerData.GetEntityAsync(bigCustomerFreightConfigurationRequestDto.BigCustomerId).GetAwaiter().GetResult() ?? throw new ArgumentException("大客户信息不存在!");
            var agentEntity = agentRecordData.GetEntityByCompanyId(bigCustomerEntity.CompanyId).GetAwaiter().GetResult() ?? throw new ArgumentException("大客户归属代理商不存在!");
            var freightConfigurationEntity = freightConfigurationData.GetByIdAsync(bigCustomerFreightConfigurationRequestDto.FreightConfigurationId).GetAwaiter().GetResult() ?? throw new ArgumentException("运费配置不存在!");

            BigCustomerFreightConfigurationEntity entity = bigCustomerFreightConfigurationRequestDto.As< BigCustomerFreightConfigurationEntity>();
            entity.FreightConfigurationId = bigCustomerFreightConfigurationRequestDto.FreightConfigurationId;
            entity.CompanyId = bigCustomerEntity.CompanyId;
            entity.AgentRecordId = agentEntity.AgentRecordId;

            entity.CreateAdminId = admin.AdminId;
            entity.CreateAdminName = admin.FullName;
            entity.CreateTime = DateTime.Now;

            return bigCustomerFreightConfigurationData.InsertAsync(entity).GetAwaiter().GetResult().As<BigCustomerFreightConfigurationDto>();
        }
        #endregion

        #region update
        public BigCustomerFreightConfigurationDto Update(BigCustomerFreightConfigurationPutDto bigCustomerFreightConfigurationPutDto, AdminDto admin)
        {
            var entity = bigCustomerFreightConfigurationData.GetEntityAsync(bigCustomerFreightConfigurationPutDto.BigCustomerFreightConfigurationId).GetAwaiter().GetResult() ?? throw new ArgumentException("要修改的记录不存在!");

            entity.StartPrice = bigCustomerFreightConfigurationPutDto.StartPrice;
            entity.StartMileage = bigCustomerFreightConfigurationPutDto.StartMileage;
            entity.MileagePrice = bigCustomerFreightConfigurationPutDto.MileagePrice;

            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            entity.UpdateTime = DateTime.Now;

            this.redisService.DeleteMultipleKey(key, false);
            return bigCustomerFreightConfigurationData.UpdateAsync(entity).GetAwaiter().GetResult().As<BigCustomerFreightConfigurationDto>();

        }
        #endregion

        #region Delete
        public void Delete(long bigCustomerFreightConfigurationId)
        {
            this.redisService.DeleteMultipleKey(key, false);
            bigCustomerFreightConfigurationData.DeleteAsync(bigCustomerFreightConfigurationId).GetAwaiter().GetResult();

        }
        #endregion

        #region GetByBigCustomerFreightConfigurationId
        public BigCustomerFreightConfigurationDto GetById(long bigCustomerFreightConfigurationId)
        {
            string cacheKey = this.redisService.GetKey(key, bigCustomerFreightConfigurationId);


            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var a = bigCustomerFreightConfigurationData.GetEntityAsync(bigCustomerFreightConfigurationId).GetAwaiter().GetResult();
                return a.As<BigCustomerFreightConfigurationDto>();
            }
            , TimeSpan.FromDays(1));
            return result.As<BigCustomerFreightConfigurationDto>();
        }
        #endregion

        #region GetByBigCustomerFreightConfigurationId
        public BigCustomerFreightConfigurationViewDto GetViewById(long bigCustomerFreightConfigurationId)
        {
            string cacheKey = this.redisService.GetKey(key, bigCustomerFreightConfigurationId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var a = bigCustomerFreightConfigurationData.GetViewEnitiyAsync(bigCustomerFreightConfigurationId).GetAwaiter().GetResult();
                return a.As<BigCustomerFreightConfigurationViewDto>();
            }
            , TimeSpan.FromDays(1));
            return result.As<BigCustomerFreightConfigurationViewDto>();
        }
        #endregion

        #region GetList  获取大客户运费配置记录--[分页]
        public PageableList<BigCustomerFreightConfigurationViewDto> GetList(AdminDto admin,long? bigCustomerId, int? vehicleTypeId, int? vehicleCategoryReferenceId, string provinceCode, string cityCode, string countyCode, OrderSourceType? sourceType, FreightType? freightType, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key,admin.CompanyId, bigCustomerId, vehicleTypeId, vehicleCategoryReferenceId, provinceCode, cityCode, countyCode, sourceType, freightType, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                int fCount = 0;
                IEnumerable<BigCustomerFreightConfigurationViewEntity> fList;

                (fList, fCount) = bigCustomerFreightConfigurationData.GetListAsync(admin.CompanyId,null,bigCustomerId, vehicleTypeId, vehicleCategoryReferenceId, provinceCode, cityCode, countyCode, sourceType, freightType, pageIndex, pageSize).GetAwaiter().GetResult();

                return new PageableList<BigCustomerFreightConfigurationViewDto>()
                {
                    Count = fCount,
                    Items = fList.As<IEnumerable<BigCustomerFreightConfigurationViewDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now,
                };
            }, TimeSpan.FromDays(30));
            return result;
        }
        #endregion

        public BigCustomerFreightConfigurationDto GetByFreightId(long freightConfigurationId,long bigCustomerId)
        {
            string cacheKey = this.redisService.GetKey(key, freightConfigurationId, bigCustomerId);

            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var a = bigCustomerFreightConfigurationData.GetEntityAsync(freightConfigurationId, bigCustomerId).GetAwaiter().GetResult();
                return a.As<BigCustomerFreightConfigurationDto>();
            }
            , TimeSpan.FromDays(1));
            return result.As<BigCustomerFreightConfigurationDto>();
        }
    }
}